﻿using Module.NumberGame.Common;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace Module.NumberGame.ViewModels
{
    public class NumberGameAppViewModel : BindableBase
    {
        private readonly IEventAggregator Ea;
        public DelegateCommand ResetCommand { get; private set; }
        public DelegateCommand MoveTest { get; private set; }
        const int TotalRow = 5;
        const int MaxNumber = 4;
        int[,] BoardData = new int[TotalRow, TotalRow];

        private int score;
        public int Score
        {
            get { return score; }
            set { SetProperty(ref score, value); }
        }

        private bool pieceMoveLeft;
        public bool PieceMoveLeft
        {
            get { return pieceMoveLeft; }
            set { SetProperty(ref pieceMoveLeft, value); }
        }
        private bool pieceMoveRight;
        public bool PieceMoveRight
        {
            get { return pieceMoveRight; }
            set { SetProperty(ref pieceMoveRight, value);}
        }
        private bool pieceMoveUp;
        public bool PieceMoveUp
        {
            get { return pieceMoveUp; }
            set { SetProperty(ref pieceMoveUp, value); }
        }
        private bool pieceMoveDown;
        public bool PieceMoveDown
        {
            get { return pieceMoveDown; }
            set { SetProperty(ref pieceMoveDown, value); }
        }
        
        public NumberGameAppViewModel(IEventAggregator ea)
        {
            this.Ea = ea;
            Ea.GetEvent<NumberMessageEvent>().Subscribe(MouseMoveProcess,ThreadOption.UIThread);
            ResetCommand = new DelegateCommand(ResetButtonClick);
            for (int i = 0; i < TotalRow; i++)
                for (int j = 0; j < TotalRow; j++)
                {
                    BoardData[i, j] = 0;
                    if (i is > 0 and < 4 && j is > 0 and < 4) BoardData[i, j] = 1;
                }
            Score = 0;
            PieceMoveDown = false; PieceMoveLeft = false; PieceMoveRight = false; PieceMoveUp = false;
        }

        private void ResetButtonClick()
        {
            //Ea.GetEvent<NumberMessageEvent>().Publish(new NumberMessage() { Content = "ResetGame" });
        }
        private void ReStart()
        {
            for (int i = 0; i < TotalRow; i++)
            {
                for (int j = 0; j < TotalRow; j++)
                {
                    BoardData[i, j] = 0;
                    if (i is > 0 and < 4 && j is > 0 and < 4) BoardData[i, j] = 1;
                }
            }
            Score = 0;
        }
        private void MouseMoveProcess(NumberMessage obj)
        {
            ProcessPieces(obj.MouseDisX, obj.MouseDisY);
            Ea.GetEvent<NumberBoardDataEvent>().Publish(new NumberMessage() { BoardDataMessage = BoardData });
        }


        private void ProcessPieces(double disx, double disy)
        {
            int[] resl = new int[TotalRow];
            Random randnumber = new();
            int newNum = randnumber.Next(1, MaxNumber);
            PieceMoveDown= false; PieceMoveLeft = false; PieceMoveRight = false; PieceMoveUp =false;

            if (Math.Abs(disx) > Math.Abs(disy)) // 水平移动量大于垂直移动量时，只水平移动
            {
                if (disx < 0) // 向左移动
                {
                    PieceMoveLeft = true;
                    
                }
                else // 向右移动
                {
                    PieceMoveRight = true;
                   
                }
            }
            else // 垂直移动
            {
                if (disy < 0) // 向上移动
                {
                    PieceMoveUp = true;
                    
                }
                else // 向下移动
                {
                    PieceMoveDown = true;
                    
                }
            }

        }
    }
}
